/*
 * @lc app=leetcode.cn id=206 lang=javascript
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
// var reverseList = function(head) {
//   if(!head) return null;
//   let pre = null, cur = head;
//   while(cur) {
//     [cur.next, pre, cur] = [pre, cur, cur.next]
//     // let next = cur.next;
//     // cur.next = pre;
//     // pre = cur;
//     // cur = next;
//   }
//   return pre;
// };

/**
 * 复习 2021-08-04
 */
var reverseList = function (head) {
  if (!head) return null;
  let pre = null; cur = head;
  while (cur) {
    [cur.next, pre, cur] = [pre, cur, cur.next];
  }
  return pre;
};

var reverseList = function (head) {
  let ret = null;

  var reverse = function (header) {
    if (!header || !header.next) {
      ret = header
      return;
    };
    // 记录最后的节点
    ret = header.next;

    reverse(header.next);
    // 下面两行代码在递归回溯阶段执行
    header.next.next = header;
    header.next = null;
  }

  reverse(head);
  return ret;
};

// 一起学算法-1
var reverseList = function (head) {
  if (!head || !head.next) return head;
  let pre = null, cur = head;
  while (cur) {
    let next = cur.next;
    cur.next = pre;
    pre = cur;
    cur = next;
    // [cur.next, pre, cur] = [pre, cur, cur.next];
  }
  return pre;
}
// @lc code=end

